package com.kx.game.center.platform;

import java.util.List;

import com.kx.game.center.common.enums.IndexedEnum;
import com.kx.game.center.utils.EnumUtil;

/**
 * 订单状态
 * 
 * @author wenji.fan
 * @since 2013年8月7日
 * 
 */
public enum OrderStatus implements IndexedEnum {
	/** 创建 */
	CREATE(1),
	/** 成功 */
	SUCCESS(2),
	/** 已兑换 */
	EXCHANGED(3),
	/** 失败 */
	FAILED(4);
	private final int index;

	static {
		SUCCESS.setPreviousStatuses(CREATE);
		EXCHANGED.setPreviousStatuses(SUCCESS);
		FAILED.setPreviousStatuses(CREATE, SUCCESS);
	}

	/**
	 * 前置状态
	 */
	private OrderStatus[] previousStatuses;

	/**
	 * 按索引顺序存放的枚举数组
	 */
	private static final List<OrderStatus> indexes = IndexedEnum.IndexedEnumUtil
			.toIndexes(OrderStatus.values());

	OrderStatus(int index) {
		this.index = index;
	}

	public boolean canEnter(OrderStatus targetState) {
		if (targetState.previousStatuses == null) {
			return false;
		}
		for (int i = 0; i < targetState.previousStatuses.length; i++) {
			if (targetState.previousStatuses[i] == this) {
				return true;
			}
		}
		return false;
	}

	public OrderStatus[] getPreviousStatuses() {
		return previousStatuses;
	}

	public void setPreviousStatuses(OrderStatus... previousStatuses) {
		this.previousStatuses = previousStatuses;
	}

	public static OrderStatus indexOf(final int index) {
		return EnumUtil.valueOf(indexes, index);
	}

	@Override
	public int getIndex() {
		return index;
	}

}
